package com.wuzhun.wxmp.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wuzhun.wxmp.annotation.LoginRequired;
import com.wuzhun.wxmp.annotation.PageLoginRequired;
import com.wuzhun.wxmp.common.Result;
import com.wuzhun.wxmp.controller.BaseController;
import com.wuzhun.wxmp.model.dto.WechatDTO;
import com.wuzhun.wxmp.model.entity.Wechat;
import com.wuzhun.wxmp.service.IWechatService;
import org.apache.commons.lang3.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 微信公众号表 前端控制器
 * </p>
 *
 * @author renbaolin
 * @since 2022-12-03
 */
@Controller
@RequestMapping("/admin/wechat")
public class AdminWechatController extends BaseController {

    @Resource
    private IWechatService wechatService;
    
    @GetMapping("")
    @ApiOperation("微信公众号列表页面")
    @PageLoginRequired
    public ModelAndView index(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                              @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize) {
        ModelAndView modelAndView = new ModelAndView();
        Page<Wechat> wechatPage = new Page<>();
        wechatPage.setCurrent(page);
        wechatPage.setSize(pageSize);
        Page<WechatDTO> wechatDTOPage = wechatService.listByPage(wechatPage, new QueryWrapper<>());
        modelAndView.addObject("pageData", wechatDTOPage);
        return commonView(modelAndView, "admin/wechat/index");
    }

    @GetMapping("add")
    @ApiOperation("添加微信公众号")
    @PageLoginRequired
    public ModelAndView add() {
        ModelAndView modelAndView = new ModelAndView();
        return commonView(modelAndView, "admin/wechat/add");
    }

    @GetMapping("edit")
    @ApiOperation("编辑微信公众号")
    @PageLoginRequired
    public ModelAndView edit(@RequestParam("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        WechatDTO wechatDTO = wechatService.queryOne(id);
        modelAndView.addObject("wechat", wechatDTO);
        return commonView(modelAndView, "admin/wechat/edit");
    }

    @PostMapping("save")
    @ApiOperation("保存微信公众号")
    @ResponseBody
    @LoginRequired
    public Result save(@RequestBody Wechat wechat) {
        if (StringUtils.isNotEmpty(wechat.getAppId())) {
            // 添加的时候生成微信公众号平台校验的Token
            wechat.setMpToken(wechat.getAppId().substring(2));
        }
        return new Result(wechatService.saveOrUpdate(wechat));
    }

    @PostMapping("delete")
    @ApiOperation("批量删除微信公众号")
    @ResponseBody
    @LoginRequired
    public Result delete(@RequestParam(value = "idList", required = false) List<Integer> idList) {
        return new Result(wechatService.batchDelete(idList));
    }

}
